package sync.ssc;

import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sync.bean.SSCBean;
import sync.db.ConnectionPool;
import sync.db.DBHelper;

public class SyncSSC {

	private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	// 第二种官网抓取方式，没有开奖时间
	public boolean getDataFromGuanWang2()throws Exception{
		URL url = new URL("http://www.cqcp.net/game/ssc/");
		Document doc = Jsoup.parse(url, 5000);
		Element element = doc.getElementById("openlist");
		Elements uls = element.getElementsByTag("ul");
		SSCBean ssc = opsUlB(uls.get(1));
		return insertData(ssc);
	}

	// 第二种官网抓取方式（数据处理）
	private SSCBean opsUlB(Element ul){
		SSCBean ssc = new SSCBean();
		Elements lis = ul.getElementsByTag("li");
		String qihao = "20"+lis.get(0).html();
		String number = lis.get(1).html().replace("-","");
		ssc.setQihao(qihao);
		transferData(number,ssc);
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		ssc.setOpentime(format.format(date));
		return ssc;
	}
	
	// 第三种抓取方式，直接去网易杀号网站获取
	public boolean getWangyiData()throws Exception{
		URL url = new URL("http://cai.163.com/shahao/cqssc/wanwei_20.html");
		Document doc = Jsoup.parse(url, 5000);
		Elements elements = doc.getElementsByTag("tbody");
		Elements trs = elements.get(0).getElementsByTag("tr");
		Element tr = trs.get(22);
		Elements tds = tr.getElementsByTag("td");
		String qihao = "20"+tds.get(0).html();
		Elements spans = tds.get(1).getElementsByTag("span");
		int tt = Integer.parseInt(spans.get(0).text());
		int t = Integer.parseInt(spans.get(1).text());
		int h = Integer.parseInt(spans.get(2).text());
		int ten = Integer.parseInt(spans.get(3).text());
		int one = Integer.parseInt(spans.get(4).text());
		SSCBean bean = new SSCBean();
		bean.setTenthousand(tt);
		bean.setThousand(t);
		bean.setHundred(h);
		bean.setTen(ten);
		bean.setOne(one);
		bean.setQihao(qihao);
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		bean.setOpentime(format.format(date));
		return insertData(bean);
	}
	
	// 先查询当前数据库最新的数据，匹配之后再入库
	private boolean insertData(SSCBean bean){
		String newQh = DBHelper.getLastRecordQihao();
		// 如果期号不一样 则代表更新下来的数据是最新的，插入数据库
		if(!bean.getQihao().equals(newQh)){
			return DBHelper.insertSSC(bean);
		}
		return false;
	}
	
	private void transferData(String haoma,SSCBean bean){
		int tt = Integer.parseInt(haoma.substring(0,1));
		int t = Integer.parseInt(haoma.substring(1,2));
		int h = Integer.parseInt(haoma.substring(2,3));
		int ten = Integer.parseInt(haoma.substring(3,4));
		int one = Integer.parseInt(haoma.substring(4,5));
		bean.setTenthousand(tt);
		bean.setThousand(t);
		bean.setHundred(h);
		bean.setTen(ten);
		bean.setOne(one);
	}

}
